<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Request;

/**
 * 优惠券库管理
 *
 * @icon fa fa-circle-o
 */
class Coupon extends Backend
{
    
    /**
     * Coupon模型对象
     * @var \app\admin\model\Coupon
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Coupon;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    //优惠券总表列表记录
    public function index(){

        $couponRes=db("coupon")->field("*")->paginate(30);
        $page=$couponRes->render();
        $this->assign([
            "couponRes"=>$couponRes,
            "page"=>$page,
        ]);



        return $this->view->fetch();
    }
//优惠券总表添加
    public function coupon_add(){

     if(Request()->isPost()){
         $data=input('post.');
         $data['start_time']=$_POST['start_time'] ? strtotime($_POST['start_time']) : '';
         $data['end_time']=$_POST['end_time'] ? strtotime($_POST['end_time']) : '';
         $add=db("coupon")->insert($data);
         if($add){
             $this->success('添加优惠券成功','index');
         }else{
             $this->error('添加优惠券失败');
         }
     }
        $op=session('admin');
        $this->assign([
            'admin'=>$op,
        ]);
        return $this->view->fetch();
    }

    public function coupon_edit($id){

       /// dump($_POST);
        if(Request()->isPost()){
            $data=input('post.');
            $data['start_time']=$_POST['start_time'] ? strtotime($_POST['start_time']) : '';
            $data['end_time']=$_POST['end_time'] ? strtotime($_POST['end_time']) : '';
            $add=db("coupon")->update($data);
            if($add){
                $this->success('修改优惠券成功','index');
            }else{
                $this->error('修改优惠券失败');
            }
        }
        $couponRes=db("coupon")->where("id","{$id}")->find();
        $op=session('admin');
        $this->assign([
            'admin'=>$op,
            'couponRes'=>$couponRes,
        ]);
        return $this->view->fetch();
    }

    public function coupon_product($cid){

        //dump($cid);
        $listPrc=db("product")->field("id,p_no,s_name,p_name")->select();
      //  dump($listPrc);
        $this->assign([
            'listPrc'=>$listPrc,
            'cid'=>$cid,
        ]);

        return $this->view->fetch();
    }

    public function coupon_save(){

        //dump($_POST);
        $id=$_POST['id'];
        $cid=$_POST['cid'];
        $res=db("product")->field("s_name,id")->where("id",$id)->find();
        if(!$res['id']){
            echo 0; die;
        }
        $data=[
            'coupon_id'=>$cid,
            'product_id'=>$id,
            'product_name'=>$res['s_name'],
        ];
        $where['coupon_id']=$cid;
        $where['product_id']=$id;
        $cRes=db("coupon_product")->where($where)->count();

        $coupon_r=db("coupon")->field("producttype")->where("id",$cid)->find();
        if(!$cRes){
            $coupon=db("coupon_product")->insert($data);
           if($coupon_r['producttype']=='0'){
               $c_r['producttype']=1;
                db("coupon")->where("id",$cid)->update($c_r);
            }
            if($coupon){
                echo 1;
            }else{
                echo 2;
            }
        }else{
            echo 3;
        }
    }


    public function coupon_product_list($cid){
        //dump($cid);
        $where['coupon_id']=$cid;
        $list=db("coupon_product")->where($where)->select();

        $this->assign("list",$list);
        $this->assign("cid",$cid);

        return $this->view->fetch();
    }

    public function coupon_save_del(){
        ///dump($_POST);
        $cid=$_POST['cid'];
        $where['id']=$_POST['id'];
        $cRes=db("coupon_product")->where($where)->delete();
        if($cRes){
            $count=db("coupon_product")->where("coupon_id=$cid")->count();
            if($count==0){
                $c_r['producttype']=0;
                db("coupon")->where("id",$cid)->update($c_r);
            }
            echo 1;
        }else{
            echo 0;
        }

    }



    

}
